package com.yoda.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.constant.MessageConstant;
import com.yoda.context.BaseContext;
import com.yoda.dto.ArticleDTO;
import com.yoda.entity.YddArticle;
import com.yoda.entity.YddCate;
import com.yoda.entity.YddTag;
import com.yoda.mapper.YddArticleMapper;
import com.yoda.mapper.YddCateMapper;
import com.yoda.result.PageResult;
import com.yoda.result.Result;
import com.yoda.service.IYddArticleService;
import com.yoda.vo.ArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements IYddArticleService {

    @Autowired
    private YddArticleMapper yddArticleMapper;

    @Autowired
    private YddCateMapper yddCateMapper;

    /**
     * 文章列表-分页
     *
     * @param articleDTO
     * @return
     */
    @Override
    public Result pageList(ArticleDTO articleDTO) {
        if (articleDTO.getPage()==null){
            articleDTO.setPage(String.valueOf(1));
        }
        if (articleDTO.getLimit()==null){
            articleDTO.setLimit(String.valueOf(5));
        }
        //创建条件
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(articleDTO.getTitle()!=null,YddArticle::getTitle,articleDTO.getTitle())
                .like(articleDTO.getCateid()!=null,YddArticle::getCateid,articleDTO.getCateid());

        String start = articleDTO.getStart_time().toString();
        String end = articleDTO.getEnd_time().toString();
        wrapper.between(start!=null && start != ""&&end!=null && end!="",YddArticle::getCreateDate,start,end);
        IPage<YddArticle> page = new Page<YddArticle>(Long.valueOf(articleDTO.getPage()),Long.valueOf(articleDTO.getLimit()));
        IPage<YddArticle> yddArticleIPage = yddArticleMapper.selectPage(page, wrapper);

        List<YddArticle> records = yddArticleIPage.getRecords();
        List<ArticleVo> voList =new ArrayList<>();
        records.forEach(yddArticle -> {
            ArticleVo vo = new ArticleVo();
            BeanUtils.copyProperties(yddArticle,vo);
            YddCate yddCate = yddCateMapper.selectById(vo.getCateid());
            vo.setCatename(yddCate.getCatename());
            voList.add(vo);
        });
        PageResult pageResult = new PageResult();
        pageResult.setData(voList);
        pageResult.setCurrentPage( yddArticleIPage.getCurrent());
        pageResult.setPageSize(yddArticleIPage.getSize());
        pageResult.setTotalPages(yddArticleIPage.getPages());
        pageResult.setCount(yddArticleIPage.getTotal());

        return Result.success(pageResult);
    }

    /**
     * 文章添加
     *
     * @param yddArticle
     * @return
     */
    @Override
    public Result add(YddArticle yddArticle) {

        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(yddArticle.getTitle()!=null,YddArticle::getTitle,yddArticle.getTitle());
        List<YddArticle> list = list(wrapper);
        if (list!=null&&list.size()>0){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        yddArticle.setUpdateDate(System.currentTimeMillis());
        yddArticle.setDefaultData("1");
        yddArticle.setUserId(BaseContext.getCurrentId().intValue());
        yddArticle.setStatus(1);
        save(yddArticle);

        return Result.success();
    }

    /**
     * 根据id查询文章
     *
     * @param id
     * @return
     */
    @Override
    public Result getById(String id) {

        YddArticle yddArticle = yddArticleMapper.selectById(id);
        return Result.success(yddArticle);
    }

    /**
     * 修改文章
     *
     * @param yddArticle
     * @return
     */
    @Override
    public Result updateArticle(YddArticle yddArticle) {
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(yddArticle.getTitle()!=null,YddArticle::getTitle,yddArticle.getTitle());
        List<YddArticle> list = list(wrapper);
        if (list!=null&&list.size()>0){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
       yddArticleMapper.updateById(yddArticle);
        return Result.success();
    }

}
